home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / www / cgi-bin / tarDFList-cgi < prev    next >
Encoding:
Text File  |  1996-11-11  |  9.6 KB  |  283 lines

  1. #!/usr/bin/perl
  2.  
  3.  
  4.    # INITIAL SETUP STATEMENTS:
  5.  
  6. # This should match the mail program on your system.
  7. $mailprog = '/usr/lib/sendmail';
  8. # Who do you want the mail to get to who as...
  9. $recipient = 'DTjanitor\@sgi.com';
  10.  
  11. #get user home directory from this file
  12. eval (`cat "/tmp/.userhome_$ENV{'SERVER_PORT'}"`);
  13.  
  14. #####################################
  15. # Setup/Define Machine-Specific stuff
  16. chop($server=`hostname`);
  17. # make the $userHome/.www_6.0/.DT_DocRootFile, specified below, have it's contents
  18. # equal to the equivalent-on-your-machine of everything *inside* the
  19. # single, "'", quotes:  '$DocumentRoot="/AbsPath/to/your/DocRoot"'
  20. $DocumentRoot=$ENV{'DOCUMENT_ROOT'};
  21. &docroot_failure unless $DocumentRoot;
  22.  
  23.  
  24. # Print out a content-type for HTTP/1.0 compatibility
  25. print "Content-type: text/html\n\n";
  26.  
  27.  
  28. # 1. start by saving out HTTP env var's we're *REALLY* interested in:
  29.  
  30. #   PATH_* var's are defined in terms of the dir location where
  31. #   the html file is which invokes this cgi-bin script
  32.  
  33. # PATH_INFO is the "relative path" from Doc root (with a leading `/' char)
  34. $relpath2parentdir = $ENV{'PATH_INFO'};
  35. # PATH_TRANSLATED is the absolute path from /
  36. $abspath2parentdir = $ENV{'PATH_TRANSLATED'};
  37.  
  38. # QUERY_STRING stores the name of the directory itself who's "child dir(s)"
  39. # is(are) the one's that got stored in the $FORM array
  40. $dir2tar = $ENV{'QUERY_STRING'} ;
  41.  
  42.  
  43.  
  44. # 2. get STDIN data from form into a useable array:
  45.  
  46. # Get the input coming from the FORM as standard input:
  47. read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  48.  
  49. # Split the name-value pairs
  50. @pairs = split(/&/, $buffer);
  51.  
  52. foreach $pair (@pairs)
  53. {
  54.     ($name, $value) = split(/=/, $pair);
  55.  
  56.     # Un-Webify plus signs and %-encoding
  57.     $value =~ tr/+/ /;
  58.     $value =~ tr/\///;
  59.     $value =~ tr/*//d;
  60.     $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  61.  
  62.     # Stop people from using subshells to execute commands
  63.     # Not a big deal when using sendmail, but very important
  64.     # when using UCB mail (aka mailx).
  65.     # $value =~ s/~!/ ~!/g; 
  66.  
  67.     $form_len++;
  68.     $nameHolder = $value;
  69.     $name = "$name" . "$form_len";
  70.  
  71.     # Uncomment for debugging purposes
  72.     #print "Setting $name to $value<br>";
  73.  
  74.     # Fill up our FORM associative array of "$name" 
  75.     # indices with their respective "value pairs":
  76.     $FORM{$name} = $value;
  77.  
  78.     # But since the above doesn't seem to be working
  79.     # well yet, (1/3/95, 7:20pm), let's just get on it:
  80.     $dirs2tar = "$dirs2tar " . "$dir2tar/$value";
  81.     # Uncomment for debugging purposes
  82.     #print "\$dirs2tar now == $dirs2tar<P>";
  83. }
  84.  
  85. #    NOW SETUP PATH/FILE VARS WE'LL NEED:
  86.  
  87. # 3.  If no file/directory is selected, then give a "blank form" response:
  88. if ( $form_len == 0 ) {
  89.     &blank_response
  90. }
  91.  
  92.  
  93. # 4. tack on the ".tar.Z" file suffix for the compressed
  94. #    tar image we're going to create:
  95. $d2tz = "$dir2tar" . ".tar.Z";
  96.  
  97.  
  98. # 5. get a pid number for use as the temporary dir we'll create in
  99. #    /WHATEVER/DOCUMENT/ROOT/IS/tmp to hold the compressed tar image file
  100. #    of the actual original dir name:
  101. $tmpdir = `$DocumentRoot/toolbox/www/cgi-bin/pid` ;
  102.  
  103. # 6. create paths we'll be needing further down and in tarsend-cgi:
  104.  
  105. #    make a "return-them-to-whence-they-came-from" url string:
  106. #    FLAW:  this assumes they ALWAYS came from an index.html file
  107. #    AND, the following test covers the special-case of the cgi-bin
  108. #    "index.html" file which can't be in that dir cuz everything in
  109. #    there is executable by the server.  so the www/cgi-bin.html file
  110. #    which is in the cgi-bin parent dir, "stands in" as if it were
  111. #    cgi-bin/index.html....  so the following fixes things when that
  112. #    one special case happens to be the one being transmitted.
  113. if ($dir2tar eq "cgi-bin") {
  114.     $ret2begin = "$relpath2parentdir" . "/cgi-bin.html";
  115. } else {
  116.     $ret2begin = "$relpath2parentdir" . "/$dir2tar/" . "index.html";
  117. }
  118.  
  119. #    make absolute path & path/file strings as well as
  120. #    a relative path/file string:
  121. $abstmpdirpath = "$DocumentRoot/tmp/" . "$tmpdir" ;
  122. $abstmpdirpathNfile = "$DocumentRoot/tmp/" . "$tmpdir" . "/$d2tz" ;
  123. $reltmpdirpathNfile = "/tmp/" . "$tmpdir" . "/$d2tz" ;
  124.  
  125.  
  126. # 7. create the complete set of shell commands to:
  127. #    make the temporary directory
  128. #    change directory to the parent of the dir to tar up
  129. #    create the tar image of dir we want and pipe it to compress
  130. #    then redirecting compress's output to stdout to the absolute
  131. #    temp file/path name:
  132. chop($cgi_bin = `dirname $0`);
  133. $maketar = "mkdir $abstmpdirpath; cd $abspath2parentdir; tar cLf - $dirs2tar | $cgi_bin/DTcompress > $abstmpdirpathNfile";
  134.  
  135.  
  136. # 8. run the "maketar" command inside the system() routine
  137. #    (0 == success, non-0 == failure)
  138. $res = system ("$maketar");
  139.  
  140.  
  141. # 9. test the result of system to confirm the tmp file was
  142. #    successfully made:
  143. if ($res == 0) {
  144.  
  145.   # we succeeded, so print out success header
  146.   &header_success ;
  147.  
  148.   # 10 get "ls -l" readout for size of this file:
  149.   @list = split(/[ \t\n]+/, `ls -l $abstmpdirpath/$d2tz`);
  150.   if ($#list == 8) {
  151.       $size = $list[4];
  152.   } else {
  153.       $size = $list[3];
  154.   }
  155.   close(LSLONG);
  156.  
  157.   # 11. construct the guts of the anchor pointing to the tmp file:
  158.   $anchor = "\"$reltmpdirpathNfile\"";
  159.  
  160.   # 12. construct the transmit action used in #13:
  161.   $urlline = "<FORM ACTION=\"/toolbox/www/cgi-bin/sendrm-cgi?$abstmpdirpathNfile\" METHOD=\"POST\"> <INPUT TYPE=\"submit\" VALUE=\"Transmit\"> the compressed tar image</FORM><br>\n";
  162.  
  163.   # 13. AND FINALLY, create the final html-crap seen in the body of this doc:
  164.   $rmcmd = "$abstmpdirpath";
  165.   print "The size of the compressed tar image you are about to transmit is  ";
  166.   print "<b>$size BYTES</b><br>";
  167.   print $urlline, "<br>\n";
  168.   print "FYI:  <i>Depending on which broswer yer using</i>, when prompted \n";
  169.   print " with the \"Save As\" window, if the filename is missing, it's \n";
  170.   print " intended name should be: <h2>$dir2tar.tar</h2>\n";
  171.  
  172.   print "<pre> \n\n </pre>";
  173.  
  174. } else {
  175.  
  176.   open (INFILE, "> /usr/tmp/tarDTList-cgi.Failure");
  177.   $val = "system call for maketar failed returning:  $res\n";
  178.   syswrite(INFILE,$val,length($val));
  179.   syswrite(INFILE,$maketar,length($maketar));
  180.   $val = "\n";
  181.   syswrite(INFILE,$val,length($val));
  182.   close (INFILE);
  183.  
  184.   # we failed, so print out failure header
  185.   &header_failure ;
  186.   
  187.   print "Oh-oh -- something messed-up with the system call ";
  188.   print "to generate the compressed tar archive. \n";
  189.   print "<p>\n";
  190.   print "See <b>/usr/tmp/tarDTList-cgi.Failure</b> for more information.\n";
  191.   print "<p>\n";
  192.   print "pleeze send ";
  193.   print "mail to ";
  194.   print "<a href=\"mailto:DTjanitor\@sgi.com\"><b>DTjanitor\@sgi.com</b> ";
  195.   print "</a>(this link <i>doesn't work</i> in xmosaic)--be sure \n";
  196.   print "to include yer own e-mail address and <i>where you were</i> ";
  197.   print "when you attempted to initiate generation of the compressed ";
  198.   print "tar file.  many thanx from the webDT tenderfoot...";
  199. }
  200.  
  201. print "<hr size=\"6\">\n";
  202. print "</body>\n";
  203. print "</html>\n";
  204.  
  205.  
  206.  
  207.  
  208. # ------------------------------------------------------------
  209. # subroutine blank_response
  210. sub blank_response
  211. {
  212.     print "<Head><Title>Sorry:  No Selection Made--Need At Least 1 Item</Title></Head>";
  213.     print "<Body><H2>Error in processing directory(s) selection</H1>";
  214.     print "Your request could not be processed because ";
  215.     print " you did not specify at least one directory name.<p>";
  216.  
  217.     print "<b>Please go <b>\"Back\"</b> and select at least one";
  218.     print " directory name.</b>";
  219.     exit;
  220. }
  221.  
  222.  
  223.  
  224. # *********************************************************
  225. # subroutine for successful compressed-tar file generation:
  226. # lay in the html header/beginning text stuff:
  227. sub header_success
  228. {
  229.  
  230.     print "<html><head><title>DT cgi generation of compressed-tar-img of Multiple Files/Dirs SUCCESSFUL</title></head><body>";
  231.     print "<h1>Successful Generation of Multiple -Files &| -Directories Compressed Tar Image</h1>";
  232.     &navigator_line
  233. }
  234.  
  235.  
  236. sub docroot_failure
  237. {
  238.     print "<html><head><title>DOCROOT environment variable not set FAILURE</title></head><body>";
  239.     print "<h1>DOCROOT environment variable not set FAILURE</h1>";
  240.     print "<p>\n";
  241.     foreach $f (`env`) {
  242.     print "<br>$f";
  243.     }
  244.     print "<p>\n";
  245.     &navigator_line;
  246.   print "pleeze send ";
  247.   print "mail to ";
  248.   print "<a href=\"mailto:DTjanitor\@sgi.com\"><b>DTjanitor\@sgi.com</b> ";
  249.   print "</a>(this link <i>doesn't work</i> in xmosaic)--be sure \n";
  250.   print "to include yer own e-mail address and <i>where you were</i> ";
  251.   print "when you attempted to initiate generation of the compressed ";
  252.   print "tar file.  many thanx from the webDT tenderfoot...";
  253.   exit;
  254. }
  255.  
  256.  
  257. # ************************************************************
  258. # subroutine header for failed compressed-tar file generation:
  259. # lay in the html header/beginning text stuff:
  260. sub header_failure
  261. {
  262.     print "<html><head><title>DT cgi generation of compressed-tar-img of Multiple Files/Dirs FAILURE</title></head><body>";
  263.     print "<h1>Failure for Generation of Multiple -Files &| -Directories Compressed Tar Image</h1>";
  264.     &navigator_line
  265. }
  266.  
  267.  
  268. # *********************************************************
  269. # subroutine for successful compressed-tar file generation:
  270. # lay in the html header/beginning text stuff:
  271. sub navigator_line
  272. {
  273.     print "<h3><a href=\"/toolbox/DT.html\">TOP</a> | \n";
  274.     print "<a href=\"/toolbox/www/cgi-bin/pheedbak-cgi/toolbox/www/cgi-bin?tarDFList-cgi\">Pheedbak</a> | \n";
  275.     print "<a href=\"/toolbox/DTtree.html\">Tree</a> | \n";
  276.     print "<a href=\"/toolbox/DTtopic.html\">Topic</a> | \n";
  277.     print "<a href=\"/toolbox/DTalfabetic.html\">A-Z</a> | \n";
  278.     print "<a href=\"/toolbox/www/cgi-bin/DTsearch-cgi\">Search</a> | \n";
  279.     print "<a href=\"/toolbox/DThot.html\">Hot</a> | \n";
  280.     print "<a href=\"/toolbox/DTnew.html\">New</a></h3>";
  281.     print "<hr size=\"4\">\n";
  282. }
  283.